using System;
using System.Web.Mvc;
using MvcContrib.Pagination;

namespace etcetera.Mvc.UI.Toolbar
{
    public static class PagingToolStripExtensions
    {
        public static IPagerToolStrip PagerToolStrip(this HtmlHelper htmlHelper, int currentPage, int pageSize, int totalItemCount)
        {
            return new PagerToolStrip(htmlHelper.ViewContext, pageSize, currentPage, totalItemCount, null);
        }

        public static IPagerToolStrip PagerToolStrip(this HtmlHelper htmlHelper, string viewDataKey)
        {
            var pagination = htmlHelper.ViewContext.ViewData.Eval(viewDataKey) as IPagination;

            if (pagination == null)
            {
                throw new InvalidOperationException(string.Format("Item in ViewData with key '{0}' is not an IPagination.", viewDataKey));
            }

            return PagerToolStrip(htmlHelper, pagination.PageNumber, pagination.PageSize, pagination.TotalItems);
        }

        public static IPagerToolStrip PagerToolStrip<T>(this HtmlHelper htmlHelper, IPagination<T> pagination)
        {
            return PagerToolStrip(htmlHelper, pagination.PageNumber, pagination.PageSize, pagination.TotalItems);
        }
    }
}